package mrh.niubi.librarysystem.controller;

import mrh.niubi.librarysystem.common.Result;
import mrh.niubi.librarysystem.constants.Constants;
import mrh.niubi.librarysystem.pojo.Order;
import mrh.niubi.librarysystem.service.BookService;
import mrh.niubi.librarysystem.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/order")
@CrossOrigin(origins = "*")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BookService bookService;

    private String getKey(){
        String key = "";
        Random random = new Random();
        for(int i = 0;i < 6;i ++){
            key += random.nextInt(100)%10;
        }
        return key;
    }

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order){
        if(orderService.addOrder(order) == 1 && bookService.subNum(order.getBookId(),order.getNum()) == 1){
            return Result.success();
        }
        else {
            return Result.error(Constants.CODE_500,"服务器添加失败");
        }
    }

    @GetMapping("/getOrdersByUsername")
    public Result getOrdersByUsername(@RequestParam String username){
        return Result.success(orderService.getOrdersByUsername(username));
    }

    @GetMapping("/getWeChatPay")
    public Result getWeChatPay(@RequestParam String id){
        return Result.success();
    }

    @GetMapping("/cheatPay")
    public Result cheatPay(@RequestParam int id){
        Order order = new Order();
        order.setId(id);
        order.setState("待借阅");
        order.setKkey(getKey());
        System.out.println(order);
        return Result.success(orderService.updateOrder(order));
    }

    @GetMapping("/delOrderById")
    public Result delOrderById(@RequestParam String id){
        Order order = orderService.getOrderById(id);
        if(order.getState().equals("待借阅")){
            //执行退款
        }
        if(orderService.delOrderById(id) == 1 && bookService.addNum(order.getBookId(),order.getNum()) == 1){
            return Result.success();
        }
        else {
            return Result.error(Constants.CODE_500,"服务器删除失败");
        }
    }

    @GetMapping("/selectOrdersByQueryinf")
    public Result selectOrdersByQueryinf(@RequestParam String queryinf,@RequestParam String username){
        return Result.success(orderService.selectOrdersByQueryinf(queryinf,username));
    }

    @GetMapping("/editOrder")
    public Result editOrder(@RequestParam String id,@RequestParam String state){
        Order order = orderService.getOrderById(id);
        order.setState(state);
        if(state.equals("已完成")){
            bookService.addNum(order.getBookId(),order.getNum());
        }
        if(orderService.updateOrder(order) == 1){
            return Result.success();
        }
        else {
            return Result.error(Constants.CODE_500,"服务器修改失败");
        }
    }


}
